const app = getApp();
import { getPickUpList } from "../../common/api";
Page({
  data: {
    pickup_name: "附近的签收点"
  },
  onLoad: function (options) {
  },
  onReady: function () {
    this.getPickUpList(0);
  },
  onShow: function () { 
    this.setData({
      printer: app.globalData.printer,
    })
  },
  //获取指定范围内的打印机
  getPickUpList(distance = 1000) {
    wx.getLocation({
      altitude: true,
      isHighAccuracy: true,
      type: "wgs84",
      success: res => {
        console.log(res)
        wx.showLoading({
          title: '加载中...',
          mask: true,
        })
        let data = {
          latitude: res.latitude,
          longitude: res.longitude,
          distance: distance,
          printerId: this.data.printer.id
        }
        this.searchPickUpRequest(data)
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  // 查询签收点列表
  searchPickUpRequest(postData) {
    getPickUpList(postData).then(res => {
        wx.hideLoading()
        if (res.code === 200) {
          console.log(res.data)
          this.setData(res.data);
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
      })
      .catch(err => {
        wx.hideLoading()
        console.log('失败', err)
      })
  },
  //选择签收点
  selectPickUp: function (e) {
    let index = e.currentTarget.dataset.index;
    let pickup_id = this.data.pickUp[index].id;
    let address = this.data.pickUp[index].address;
    let contactPhone = this.data.pickUp[index].contactPhone;
    let contactName = this.data.pickUp[index].contactName;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]  //上一个页面
    prevPage.setData({
      ["pickUpId"]: pickup_id,
      ["deliveryAddress"]: address,
      ["deliveryContact"]: contactName,
      ["deliveryPhone"]: contactPhone
    });
    prevPage.getTotalFee()
    wx.navigateBack({
      delta: 1,
    })
  },
  showCustomerQrcode(e) {
    let index = e.currentTarget.dataset.index;
    let qrcode = this.data.pickUp[index].contactPhone;
    console.log(qrcode);
    wx.makePhoneCall({
      phoneNumber: qrcode,
    })
  },
  navigatorToShop(e) {
    let index = e.currentTarget.dataset.index;
    let curr_pick_up = this.data.pickUp[index];
    console.log(curr_pick_up);
    let lat = parseFloat(curr_pick_up.lat);
    let lng = parseFloat(curr_pick_up.lng);
    wx.openLocation({
      latitude: lat,
      longitude: lng,
    })
  },
  searchPickUp() {
    let menu = ["1公里", "3公里", "5公里", "全部签收点"];
    let menuValue = ["1000", "3000", "5000", "0"];
    wx.showActionSheet({
      alertText: "搜索范围",
      itemList: menu,
      success: res => {
        this.getPickUpList(parseInt(menuValue[res.tapIndex]));
      }
    })
  }
})